<?php
/*
 * @Author: lokei
 * @Date: 2022-10-07 16:17:35
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-15 21:21:33
 * @Description: 
 */
namespace App\Http\Controllers\Pay\Withdraw;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Pay\Withdraw\RecordModel;
use App\Models\Store\StoreModel;
use Illuminate\Http\Request;

class RecordController extends Controller {
    public function ofStore(Request $request) {
        $items = RecordModel::where('store_id', '=', $request->input('store_id'))->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function list(Request $request) {
        if($request->input('store_id') != null && intval($request->input('store_id')) > 0) {
            $items = RecordModel::where('store_id', '=', $request->input('store_id'))->get();
        } else {
            $items = RecordModel::get();
        }
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function accept(Request $request) {
        $record = RecordModel::where('id', '=', $request->input('id'))->first();
        if($record->type == 'store') {
            $store = StoreModel::where('id', '=', $record->store_id)->first();
            $store->withdrawed = $store->withdrawed + $record->money;
            $store->withdrawing = $store->withdrawing  - $record->money;
            $store->save();
        }
        $record->status = '1';
        $record->save();
        return ResultTool::success();
    }
}